package zd.code.handler;
import zd.code.bean.Table;
import zd.code.constant.Constant;
import zd.code.util.FreeMarkerUtils;
import zd.code.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

public class MvcHandler {

    String templateFile = Constant.template_path;
    // 生成实体类
    public void executeModule(Table table) {
        Map input = new HashMap();
        input.put("table", table);
        input.put("package", Constant.package_name);
        input.put("lowerClassName", StringUtils.lowerName(table.getClassName()));
        String fileName = table.getClassName() + ".java";
        String savePath = Constant.model_save_path;
        String templateName = "model";
        FreeMarkerUtils.genteratorFile(input, templateFile, templateName, savePath, fileName);
    }
    // 生成Mapper映射文件
    public void executeMapper(Table table) {
        Map input = new HashMap();
        input.put("table", table);
        input.put("package", Constant.package_name);
        input.put("lowerClassName", StringUtils.lowerName(table.getClassName()));
        String fileName = table.getClassName() + "Mapper" + ".xml";
        String savePath = Constant.mapper_save_path + "//";
        String templateName = "mapper";
        FreeMarkerUtils.genteratorFile(input, templateFile, templateName, savePath, fileName);
    }
    // 生成Mapper类
    public void executeClazzMapper(Table table) {
        Map input = new HashMap();
        input.put("table", table);
        input.put("package", Constant.package_name);
        input.put("lowerClassName", StringUtils.lowerName(table.getClassName()));
        String fileName = table.getClassName() + "Mapper" + ".java";
        String savePath = Constant.class_mapper_save_path +"//";
        String templateName = "clazzMapper";
        FreeMarkerUtils.genteratorFile(input, templateFile, templateName, savePath, fileName);
    }

    // 生成Service类
    public void executeServiceMapper(Table table) {
        Map input = new HashMap();
        input.put("table", table);
        input.put("package", Constant.package_name);
        input.put("lowerClassName", StringUtils.lowerName(table.getClassName()));
        String fileName = table.getClassName() + "Service" + ".java";
        String savePath = Constant.service_save_path +"//";
        String templateName = "service";
        FreeMarkerUtils.genteratorFile(input, templateFile, templateName, savePath, fileName);
    }

    // 生成ServiceImpl类
    public void executeServiceImplMapper(Table table) {
        Map input = new HashMap();
        input.put("table", table);
        input.put("package", Constant.package_name);
        input.put("lowerClassName", StringUtils.lowerName(table.getClassName()));
        String fileName = table.getClassName() + "ServiceImpl" + ".java";
        String savePath = Constant.service_impl_save_path +"//";
        String templateName = "serviceImpl";
        FreeMarkerUtils.genteratorFile(input, templateFile, templateName, savePath, fileName);
    }

    // 生成Controller类
    public void executeControllerMapper(Table table) {
        Map input = new HashMap();
        input.put("table", table);
        input.put("package", Constant.package_name);
        String lowerClassName = StringUtils.lowerName(table.getClassName());
        String path = lowerClassName;
        String paths = path;
        if(!path.endsWith("s")){
            paths += "s";
        }
        input.put("path",path);
        input.put("paths",paths);
        input.put("lowerClassName", lowerClassName);
        String fileName = table.getClassName() + "Controller" + ".java";
        String savePath = Constant.controller_save_path +"//";
        String templateName = "controller";
        FreeMarkerUtils.genteratorFile(input, templateFile, templateName, savePath, fileName);
    }

}
